class Solution:
    def constructProductMatrix(self, grid: List[List[int]]) -> List[List[int]]:
        m = len(grid)
        n = len(grid[0])
        prod = 1
        zero_count = 0
        zero_pos = 0, 0
        for i, row in enumerate(grid):
            for j, num in enumerate(row):
                num %= 12345
                if num == 0:
                    if zero_count:
                        return [[0] * n for _ in range(m)]
                    else:
                        zero_count += 1
                        zero_pos = i, j
                else:
                    prod *= num
        

        p = [[0] * n for _ in range(m)]
        if zero_count:
            i, j = zero_pos
            p[i][j] = prod % 12345
            return p
        for i in range(m):
            for j in range(n):
                p[i][j] = (prod // (grid[i][j] % 12345)) % 12345
        return p
    
        